package Thread;

import java.util.concurrent.Semaphore;

public class Demo44 {
    private static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        //把只有一个资源的信号量当锁使用，更灵活
    Semaphore semaphore = new Semaphore(1);
    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                semaphore.acquire();
                count++;
                semaphore.release();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                try {
                    semaphore.acquire();
                    count++;
                    semaphore.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        thread2.start();
        thread.join();
        thread2.join();
        System.out.println(count);
    }
}
